package com.example.fb.activity;

import java.math.BigInteger;
import java.util.ArrayList;

import com.example.fb.R;
import com.example.fb.adapter.FbDataAdapter;
import com.example.fb.sys.MySys;
import com.example.fb.tools.Tools;

import android.os.Bundle;
import android.R.integer;
import android.app.Activity;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ListView;

public class MainActivity extends Activity {
	private ListView fbList;
	public static ArrayList<BigInteger> mFbList = new ArrayList<BigInteger>();
	private int mSint = 20;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initView();
		initData();
	}

	private void initData() {
		// TODO Auto-generated method stub
		// 初始化数据
		for (int i = 0; i < 20; i++) {
			mFbList.add(Tools.getFb(i));
		}
		// list适配器
		final FbDataAdapter fbAdapter = new FbDataAdapter(this);
		fbList.setAdapter(fbAdapter);
		fbAdapter.notifyDataSetChanged();
		
		fbList.setOnScrollListener(new OnScrollListener() {

			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				// TODO Auto-generated method stub
			}

			@Override
			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {
				// TODO Auto-generated method stub
				if (firstVisibleItem+visibleItemCount == totalItemCount) {
					mFbList.add(mFbList.get(mFbList.size()-1).add(mFbList.get(mFbList.size()-2)));
					mSint++;
					fbAdapter.notifyDataSetChanged();
				}
			}
		});
		//倒叙
		fbList.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				float xDown = 0;
				float xUp = 0;
				float yDown = 0;
				float yUp = 0;
				if(event.getAction()  == event.ACTION_DOWN){
					xDown = event.getX();
					yDown = event.getY();
				}else if(event.getAction()  == event.ACTION_UP){
					xUp = event.getX();
					yUp = event.getY();
				}
				if((xUp-xDown)>(yUp-yDown)&&(xUp-xDown)>0){
					MySys.isTurn= true;
					fbAdapter.notifyDataSetChanged();
				}
				if((xUp-xDown)<(yUp-yDown)&&(xUp-xDown)<0){
					MySys.isTurn= false;
					fbAdapter.notifyDataSetChanged();
				}
				return false;
			}
		});
	}

	private void initView() {
		// TODO Auto-generated method stub
		fbList = (ListView) findViewById(R.id.fb_list);
		
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

}
